package action;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import service.MsgArrive;
import util.AESUtils;
import util.MyUtil;

/**
 * Created by zhangting on 02/04/2019.
 */
public class DoAction {

    private static MsgArrive msgArrive;
    public static String key=MyUtil.createRandomCharData(16);



    //yxapi/getBody
    public String getBody(String body) {
        System.out.println("===收到body: " + body);
        try {
            body = AESUtils.aesDecrypt(body,key);
            System.out.println("===decrypted body: " + body);
            JSONObject json = JSON.parseObject(body);
            String msgtype = json.getString("msgtype");
            String fromqq = json.getString("fromqq");
            String toqq = json.getString("toqq");
            String msg = json.getString("msg");
            String groupqq = json.getString("groupqq");

            if ("friend".equalsIgnoreCase(msgtype)) {
                msgArrive.receivedFriendMsg(fromqq, toqq, msg);
            } else if ("group".equalsIgnoreCase(msgtype)) {
                msgArrive.receivedGroupMsg(fromqq, groupqq, msg);
            }else if("temp".equalsIgnoreCase(msgtype)){
                msgArrive.receivedTempMsg(fromqq,toqq,groupqq,msg);
            }else if("offline".equalsIgnoreCase(msgtype)){
                msgArrive.offline();
            }
            return "ok";
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    //yxapi/seed
    public String getKey() {
        System.out.println("get key :"+key);
        return key;
    }


    public static void setMsgArrive(MsgArrive obj) {
        DoAction.msgArrive = obj;
    }
}
